#include "lrcpage.h"
#include "ui_lrcpage.h"

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    // 设置窗口无标题栏
    setWindowFlag(Qt::FramelessWindowHint);
    // 初始化动画对象
    animal = new QPropertyAnimation(this ,"geometry",this);
    animal->setDuration(500); // 时间
    animal->setStartValue(QRect(10,10,width(),height()));
    animal->setEndValue(QRect(10,10+height(),width(),height()));
    // 下拉按钮关联事件
    connect(ui->hideBtn,&QPushButton::clicked,this,[=]{
        // 隐藏窗口
        animal->start();
    });
    connect(animal,&QPropertyAnimation::finished,this,[=]{
       hide();
    });
    ui->hideBtn->setIcon(QIcon(":/images/star.png"));



}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrcFile(const QString &lrcFilePath)
{
    // 打开歌词文件
    QFile file(lrcFilePath);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "打开文件失败" << lrcFilePath ;
        return false; // 打开文件失败
    }

    lrcWordLines.clear();
    while(!file.atEnd())
    {
        QString lrcLineWord = file.readLine(1024);

        // 解析歌词
        // [ aa:bb:cc] 歌词
        int start = 0;
        int end = 0;
        end = lrcLineWord.indexOf(']',start);
        QString lrcTime = lrcLineWord.mid(start,end-start+1);
        QString lrcWord = lrcLineWord.mid(end+1,lrcLineWord.size() - end -1 -1);

        qint64 lineTime = 0;
        // 分钟
        start = 1;
        end = lrcTime.indexOf(':',start);
        lineTime += lrcTime.mid(start,end-start).toInt()*60*1000; // 将分钟转为毫秒

        // 秒钟
        start = end + 1;
        end = lrcTime.indexOf('.',start);
        lineTime += lrcTime.mid(start,end-start).toInt()*1000; // 将秒转为毫秒

        // 毫秒
        start = end + 1;
        end = lrcTime.indexOf('.',start);
        lineTime += lrcTime.mid(start,end-start).toInt(); // 毫秒

        lrcWordLines.push_back(LrcWordLine(lineTime,lrcWord));// 构造歌词内容及时间存储 并放入容器中
    }
    for(auto e : lrcWordLines)
    {
        qDebug() << e.lrcTime << ":" << e.lrcText;
    }
    return true;

}

void LrcPage::showLrcWordLine(qint64 time)
{
    int index = getLrcWordLineIndex(time);
    if(-1 == index)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCen->setText("当前无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(gerLrcWordByIndex(index-3));
        ui->line2->setText(gerLrcWordByIndex(index-2));
        ui->line3->setText(gerLrcWordByIndex(index-1));
        ui->lineCen->setText(gerLrcWordByIndex(index));
        ui->line4->setText(gerLrcWordByIndex(index+1));
        ui->line5->setText(gerLrcWordByIndex(index+2));
        ui->line6->setText(gerLrcWordByIndex(index+3));
    }
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    // 将参数和歌词里面建立联系
    if(lrcWordLines.isEmpty())
    {
        // 歌词为空
        return -1;
    }
    if(time <= lrcWordLines[0].lrcTime)
    {
        return 0;
    }
    for(int i = 1; i < lrcWordLines.size(); i++)
    {
        if(time > lrcWordLines[i-1].lrcTime && time < lrcWordLines[i].lrcTime)
        {
            return i-1;
        }
    }
    return lrcWordLines.size()-1;
}

QString LrcPage::gerLrcWordByIndex(int index)
{
    if(index < 0 || index >= lrcWordLines.size())
    {
        return "";
    }
    return lrcWordLines[index].lrcText;
}
